package com.foodorder.service;

import com.foodorder.entity.Banner;
import com.foodorder.repository.BannerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * Banner服务类
 */
@Service
@RequiredArgsConstructor
public class BannerService {
    
    private final BannerRepository bannerRepository;
    
    /**
     * 获取所有激活的Banner（前端展示用）
     */
    public List<Banner> getActiveBanners() {
        return bannerRepository.findByStatusOrderBySortOrderAsc(Banner.BannerStatus.ACTIVE);
    }
    
    /**
     * 分页查询Banner（管理端用）
     */
    public Page<Banner> findBanners(String title, Banner.BannerStatus status, Pageable pageable) {
        if (title != null && !title.trim().isEmpty()) {
            if (status != null) {
                return bannerRepository.findByStatusAndTitleContaining(status, title.trim(), pageable);
            } else {
                return bannerRepository.findByTitleContaining(title.trim(), pageable);
            }
        } else {
            if (status != null) {
                return bannerRepository.findAll(pageable);
            } else {
                return bannerRepository.findAll(pageable);
            }
        }
    }
    
    /**
     * 根据ID查找Banner
     */
    public Optional<Banner> findById(Long id) {
        return bannerRepository.findById(id);
    }
    
    /**
     * 保存Banner
     */
    @Transactional
    public Banner save(Banner banner) {
        // 如果是新Banner且没有设置排序号，自动设置为最大值+1
        if (banner.getId() == null && banner.getSortOrder() == null) {
            Integer maxSortOrder = bannerRepository.findMaxSortOrder();
            banner.setSortOrder(maxSortOrder + 1);
        }
        return bannerRepository.save(banner);
    }
    
    /**
     * 删除Banner
     */
    @Transactional
    public void deleteById(Long id) {
        bannerRepository.deleteById(id);
    }
    
    /**
     * 更新Banner状态
     */
    @Transactional
    public void updateStatus(Long id, Banner.BannerStatus status) {
        Optional<Banner> optionalBanner = bannerRepository.findById(id);
        if (optionalBanner.isPresent()) {
            Banner banner = optionalBanner.get();
            banner.setStatus(status);
            bannerRepository.save(banner);
        }
    }
    
    /**
     * 更新排序
     */
    @Transactional
    public void updateSortOrder(Long id, Integer sortOrder) {
        Optional<Banner> optionalBanner = bannerRepository.findById(id);
        if (optionalBanner.isPresent()) {
            Banner banner = optionalBanner.get();
            banner.setSortOrder(sortOrder);
            bannerRepository.save(banner);
        }
    }
    
    /**
     * 批量删除
     */
    @Transactional
    public void deleteByIds(List<Long> ids) {
        bannerRepository.deleteAllById(ids);
    }
} 